package com.wh.blockquene;

/**
 * 
 *  Class Name: TestWaitAndNotify.java
 *  Description: 线程的等待和唤醒必须持有该对象的锁
 *  @author wanghao  DateTime 2016年7月26日 上午11:45:05 
 *  @company hongbo
 *  @email 1228675455@qq.com
 *  
 */
public class TestWaitAndNotify implements Runnable{
	
	private StudentDao studentDao;
	
	public TestWaitAndNotify(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public static void main(String[] args) {
		
		final StudentDao dao = new StudentDao();
		
		final Thread thread1 = new Thread() {

			@Override
			public void run() {
				dao.addStudent();
			}
		};
		thread1.start();
		final Thread thread2 = new Thread() {

			@Override
			public void run() {
				dao.deleteStudent();
			}
		};
		thread2.start();
		
	}
	
	static class StudentDao {
		
		
		public synchronized void addStudent() {
			System.out.println("增加开始");
			try {
				System.out.println("线程开始等待！" + ":等待5秒钟之后放开持有的锁");
				Thread.currentThread().sleep(5000);
				this.wait();
				System.out.println("线程等待结束！");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			System.out.println("增加结束");
		}
		
		public synchronized void deleteStudent() {
			System.out.println("删除开始");
			try {
				Thread.currentThread().sleep(3000);
				this.notify();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("删除结束");
		}
	}

	@Override
	public void run() {
		//可以的入参的方式来调用程序
	}
	
}
